package com.haochang.chunkvod.view.vod.popupWindow;

import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

/**
 * 订单查询PopWindow长按操作
 * 
 * @author qintao
 * 
 */
public class LongClickPopWindow implements OnLongClickListener, OnClickListener
{

	protected DialogEnum dialogEnum = DialogEnum.SINGLE_REMINDER;
	private PopupWindow popupwindow;
	private BaseTextView tv_left;
	private BaseTextView tv_right;
	private View view_split;
	private String flow_code;
	private Object obj;
	private Context context;
	private IPopWindowLongClickListener listener;

	public void setPopWindowLongClickListener(IPopWindowLongClickListener listener)
	{
		this.listener = listener;
	}

	public LongClickPopWindow(LongClickPopWindow.DialogEnum dialogEnum, Context context, String flow_code, Object obj)
	{
		this.dialogEnum = dialogEnum;
		this.context = context;
		this.flow_code = flow_code;
		this.obj = obj;
	}

	@Override
	public boolean onLongClick(View view)
	{
		View myView = LayoutInflater.from(context).inflate(R.layout.view_long_click_popwindow, null);
		myView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		popupwindow = new PopupWindow(context);
		popupwindow.setHeight(LayoutParams.WRAP_CONTENT);
		popupwindow.setWidth(LayoutParams.WRAP_CONTENT);
		ColorDrawable cd = new ColorDrawable();
		popupwindow.setBackgroundDrawable(cd);
		popupwindow.setTouchable(true);
		popupwindow.setFocusable(true);
		popupwindow.setOutsideTouchable(true);
		popupwindow.setContentView(myView);
		tv_left = (BaseTextView) myView.findViewById(R.id.tv_left);
		tv_right = (BaseTextView) myView.findViewById(R.id.tv_right);
		view_split = myView.findViewById(R.id.view_split);
		if (dialogEnum == DialogEnum.SINGLE_BACK)
		{
			tv_right.setText("退单");
			setSingleStyle();
		} else if (dialogEnum == DialogEnum.SINGLE_REMINDER)
		{
			tv_right.setText("催单");
			setSingleStyle();
		} else if (dialogEnum == DialogEnum.SINGLE_CANCAL)
		{
			tv_right.setText("取消增送");
			setSingleStyle();
		} else if (dialogEnum == DialogEnum.DOUBLE_REMINDER_BACK)
		{
			tv_left.setText("催单");
			tv_right.setText("退单");
		} else if (dialogEnum == DialogEnum.DOUBLE_REMINDER_CANCAL)
		{
			tv_left.setText("催单");
			tv_right.setText("取消增送");
		}
		tv_left.setOnClickListener(this);
		tv_right.setOnClickListener(this);
		myView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int rootHeight = myView.getMeasuredHeight();
		int rowHeight = view.getHeight();
		int width = ScreenUtils.getScreenWidth(context) / 2;
		int viewWidth = myView.getMeasuredWidth() / 2;
		popupwindow.showAsDropDown(view, width - viewWidth, -(rowHeight + rootHeight));
		return true;
	}

	private void setSingleStyle()
	{
		tv_left.setVisibility(View.GONE);
		view_split.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v)
	{

		switch (dialogEnum)
		{
		case SINGLE_BACK:
			if (listener != null)
			{
				listener.back(obj, flow_code);
				popupwindow.dismiss();
			}
			break;
		case SINGLE_CANCAL:
			if (listener != null)
			{
				listener.cancelGive(obj, flow_code);
				popupwindow.dismiss();
			}
			break;
		case SINGLE_REMINDER:
			if (listener != null)
			{
				listener.reminder(obj, flow_code);
				popupwindow.dismiss();
			}
			break;
		case DOUBLE_REMINDER_BACK:
			if (listener != null)
			{
				if (v.getId() == R.id.tv_left)
				{
					listener.reminder(obj, flow_code);
				} else
				{
					listener.back(obj, flow_code);
				}
				popupwindow.dismiss();
			}
			break;
		case DOUBLE_REMINDER_CANCAL:
			if (listener != null)
			{
				if (v.getId() == R.id.tv_left)
				{
					listener.reminder(obj, flow_code);
				} else
				{
					listener.cancelGive(obj, flow_code);
				}
				popupwindow.dismiss();
			}
			break;

		default:
			break;
		}
	}

	public enum DialogEnum
	{
		/**
		 * 单按钮-催单
		 */
		SINGLE_REMINDER,
		/**
		 * 单按钮-退单
		 */
		SINGLE_BACK,
		/**
		 * 单按钮-取消增送
		 */
		SINGLE_CANCAL,
		/**
		 * 双按钮-催单-取消增送
		 * 
		 */
		DOUBLE_REMINDER_CANCAL,
		/**
		 * 双按钮-催单-退单
		 */
		DOUBLE_REMINDER_BACK;
	}
}
